package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.dto.DeviceLogDTO;
import com.iwomy.envpro.platform.entity.DeviceLogEntity;
import com.iwomy.envpro.platform.service.DeviceLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备报文
 *
 * @author pig
 * @date 2023-10-19 16:07:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deviceLog")
@Tag(description = "deviceLog", name = "设备报文管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DeviceLogController {

	private final DeviceLogService deviceLogService;

	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param deviceLog 设备报文
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_deviceLog_view')")
	public R getDeviceLogPage(@ParameterObject Page page, @ParameterObject DeviceLogDTO deviceLog) {
		try {
			return R.ok(deviceLogService.getDeviceLogPage(page, deviceLog));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}


	/**
	 * 通过id查询设备报文
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_deviceLog_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(deviceLogService.getById(id));
	}

	/**
	 * 新增设备报文
	 *
	 * @param deviceLog 设备报文
	 * @return R
	 */
	@Operation(summary = "新增设备报文", description = "新增设备报文")
	@SysLog("新增设备报文")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_deviceLog_add')")
	public R save(@RequestBody DeviceLogEntity deviceLog) {
		return R.ok(deviceLogService.save(deviceLog));
	}

	/**
	 * 修改设备报文
	 *
	 * @param deviceLog 设备报文
	 * @return R
	 */
	@Operation(summary = "修改设备报文", description = "修改设备报文")
	@SysLog("修改设备报文")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_deviceLog_edit')")
	public R updateById(@RequestBody DeviceLogEntity deviceLog) {
		return R.ok(deviceLogService.updateById(deviceLog));
	}

	/**
	 * 通过id删除设备报文
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除设备报文", description = "通过id删除设备报文")
	@SysLog("通过id删除设备报文")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_deviceLog_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(deviceLogService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param deviceLog 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_deviceLog_export')")
	public List<DeviceLogEntity> export(DeviceLogEntity deviceLog) {
		return deviceLogService.list(Wrappers.query(deviceLog));
	}
}